﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using AxShockwaveFlashObjects;

namespace SIGEB.Prototipo.Vista.UI
{
    public partial class ComponenteFlash : UserControl
    {
        #region CONSTRUCTOR
        public ComponenteFlash()
        {
            InitializeComponent();
        }
        #endregion

        #region METODOS

        private void InicFlash(ElementoFlash flashObj, byte[] swfFile)
        {
            MemoryStream stm = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stm);
            writer.Write(8 + swfFile.Length);
            writer.Write(0x55665566);
            writer.Write(swfFile.Length);
            writer.Write(swfFile);
            stm.Seek(0, SeekOrigin.Begin);
            flashObj.OcxState = new AxHost.State(stm, 1, false, null);
        }

        /// <summary>
        /// Modo de uso:
        /// 
        /// componenteFlash1.Cargar(SIGEB.Prototipo.Properties.Resources.flash_file);
        /// 
        /// </summary>
        public void Cargar(byte[] flash_byte_array)
        {        
            byte[] data = flash_byte_array;
            InicFlash(elemento, data);
            elemento.Menu = false;
            elemento.Play();
            elemento.BGColor = string.Concat("#", (BackColor.ToArgb() & 0x00FFFFFF).ToString("X6"));
            elemento.FSCommand += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(this.elemento_FSCommand);
        }

        public void Cargar(byte[] flash_byte_array, Color color)
        {
            byte[] data = flash_byte_array;
            InicFlash(elemento, data);
            elemento.Menu = false;
            elemento.Play();
            elemento.BGColor = string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
            elemento.FSCommand += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(this.elemento_FSCommand);
        }

        /// <summary>
        /// Para cargar desde fuera del ejecutable
        /// </summary>
        /// <param name="nombre_archivo"></param>
        private void Cargar(string nombre_archivo)
        {
            elemento.LoadMovie(0, Application.StartupPath + "\\" + nombre_archivo);
            elemento.Menu = false;
            elemento.Play();
            elemento.BGColor = string.Concat("#", (BackColor.ToArgb() & 0x00FFFFFF).ToString("X6"));
            elemento.FSCommand += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(this.elemento_FSCommand);
        }
        #endregion

        #region EVENTOS
        private void elemento_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
        {
            switch (e.command)
            {
                case "CLICK": OnClick(new EventArgs()); break;
                case "DOUBLE_CLICK": OnDoubleClick(new EventArgs()); break;
                case "MOUSE_DOWN": OnMouseDown(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0)); break;
                case "MOUSE_MOVE": OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0)); break;
                case "MOUSE_OUT": OnMouseLeave(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0)); break;
                case "MOUSE_OVER": OnMouseHover(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0)); break;
                case "MOUSE_UP": OnMouseUp(new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0)); break;
                case "TEXT_CHANGED": OnTextChanged(new EventArgs()); break;
                case "VISIBLE_CHANGED": OnVisibleChanged(new EventArgs()); break;
                case "ENTER": OnEnter(new EventArgs()); break;
                case "LEAVE": OnLeave(new EventArgs()); break;
            }
        }
        #endregion

        #region EVENT HANDLERS
        protected override void OnClick(EventArgs e)
        {
            try
            {
                base.OnClick(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            try
            {
                base.OnDoubleClick(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            try
            {
                base.OnMouseDown(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            try
            {
                base.OnMouseMove(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            try
            {
                base.OnMouseLeave(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnMouseHover(EventArgs e)
        {
            try
            {
                base.OnMouseHover(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            try
            {
                base.OnMouseUp(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            try
            {
                base.OnTextChanged(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            try
            {
                base.OnVisibleChanged(e);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void  OnEnter(EventArgs e)
        {
            try
            {
 	         base.OnEnter(e);
            }
            catch(Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

        protected override void  OnLeave(EventArgs e)
        {
            try
            {
 	         base.OnLeave(e);
            }
            catch(Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }
        #endregion

        #region ATRIBUTOS
        #endregion
    }

    public class ElementoFlash : AxShockwaveFlash
    {
        #region METODOS
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_RBUTTONDOWN)
            {
                m.Result = IntPtr.Zero;
                return;
            }
            base.WndProc(ref m);
        }
        #endregion

        #region VARIABLES
        const int WM_RBUTTONDOWN = 0x0204;
        #endregion
    }
}
